package view;

import java.util.Collection;

import net.roarsoftware.lastfm.Album;
import net.roarsoftware.lastfm.Artist;
import net.roarsoftware.lastfm.Playlist;
import net.roarsoftware.lastfm.Track;
import net.roarsoftware.lastfm.User;
import i18n.Internacionalization;

import com.google.wave.api.Blip;
import com.google.wave.api.ElementType;
import com.google.wave.api.FormElement;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

/**
 * Se encarga de crear las vistas que se muestran en Google Wave
 *
 */
public class RenderView {

	// Constantes para los formularios
	public static final String ID_INPUT_USER = "user";	
	public static final String ID_INPUT_PASS = "pass";	
	public static final String ID_BUTTON_LOGIN = "id_button_login";
	
	public static final String ID_INPUT_NAME = "name";	
	public static final String ID_INPUT_DESCRIPTION = "description";	
	public static final String ID_BUTTON_CREATE_PLAYLIST = "id_button_create_playlist";
	
	public static final String ID_INPUT_ARTIST = "artist";	
	public static final String ID_INPUT_TRACK = "track";	
	public static final String ID_BUTTON_ADD_TRACK = "id_button_add_track";
	
	public static void renderLoginView(Wavelet wavelet) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		textView.append(Internacionalization.getString("\nPor favor, introduce tus datos de conexion a Last.fm:"));		
		textView.append(Internacionalization.getString("\nUsuario: "));
		textView.appendElement(new FormElement(ElementType.INPUT, ID_INPUT_USER));
		textView.append(Internacionalization.getString("\nContraseña: "));
		textView.appendElement(new FormElement(ElementType.PASSWORD, ID_INPUT_PASS));		
		textView.appendElement(new FormElement(ElementType.BUTTON, ID_BUTTON_LOGIN, Internacionalization.getString("Conectar")));
	}
	
	public static void renderCreatePlayistView(Wavelet wavelet) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		textView.append(Internacionalization.getString("\nPor favor, introduce los datos necesarios para la creación del playlist:"));		
		textView.append(Internacionalization.getString("\nNombre: "));
		textView.appendElement(new FormElement(ElementType.INPUT, ID_INPUT_NAME));
		textView.append(Internacionalization.getString("\nDescripción: "));
		textView.appendElement(new FormElement(ElementType.INPUT, ID_INPUT_DESCRIPTION));		
		textView.appendElement(new FormElement(ElementType.BUTTON, ID_BUTTON_CREATE_PLAYLIST, Internacionalization.getString("Crear")));
	}
	
	public static void renderAddTrackView(Wavelet wavelet) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		textView.append(Internacionalization.getString("\nPor favor, introduce los datos necesario para añadir la canción a la lista:"));		
		textView.append(Internacionalization.getString("\nArtista: "));
		textView.appendElement(new FormElement(ElementType.INPUT, ID_INPUT_ARTIST));
		textView.append(Internacionalization.getString("\nCanción: "));
		textView.appendElement(new FormElement(ElementType.INPUT, ID_INPUT_TRACK));		
		textView.appendElement(new FormElement(ElementType.BUTTON, ID_BUTTON_ADD_TRACK, Internacionalization.getString("Añadir")));
	}
	
	public static void renderNeighboursView(Wavelet wavelet, Collection<User> users, String user) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		if (users.size() > 0) {
			textView.append(Internacionalization.getString("Lista de vecinos de ") + user + ":");
			textView.append("\n");
			for (User userN : users) {
				textView.append(userN.getName() + " - " + userN.getUrl());
				textView.append("\n");
			}
		} else {
			textView.append(Internacionalization.getString("No se han encontrado datos del usuario ") + user);
		}
	}
	
	public static void renderRecentTracksView(Wavelet wavelet, Collection<Track> tracks, String user) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		if (tracks.size() > 0) {
			textView.append(Internacionalization.getString("Lista de canciones recientes de ") + user + ":");
			textView.append("\n");
			for (Track track : tracks) {
				textView.append("\n" + track.getName() + " - " + track.getAlbum() + " (" + track.getArtist() + ")\n");
			}
		} else {
			textView.append(Internacionalization.getString("No se han encontrado datos del usuario ") + user);
		}
	}
	
	public static void renderTopAlbumsView(Wavelet wavelet, Collection<Album> albums, String user) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		if (albums.size() > 0) {
			textView.append(Internacionalization.getString("Top Albums de ") + user + ":");
			textView.append("\n");
			for (Album album : albums) {
				textView.append(album.getName() + " - " + album.getArtist());
				textView.append("\n");
			}
		} else {
			textView.append(Internacionalization.getString("No se han encontrado datos del usuario ") + user);
		}
		
	}
	
	public static void renderTopArtistView(Wavelet wavelet, Collection<Artist> artists, String user) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		if (artists.size() > 0) {
			textView.append(Internacionalization.getString("Top Artist de ") + user + ":");
			textView.append("\n");
			for (Artist artist : artists) {
				textView.append(artist.getName());
				textView.append("\n");
			}
		} else {
			textView.append(Internacionalization.getString("No se han encontrado datos del usuario ") + user);
		}
	}
	
	public static void renderShowPlaylistView(Wavelet wavelet, Playlist playlist) {
		Blip blip = wavelet.appendBlip();
		TextView textView = blip.getDocument();
		textView.append("\nPlaylist: " + playlist.getTitle());
		textView.append("\nTracks:");
		for (Track track : playlist.getTracks()) {
			textView.append("\n" + track.getName() + " - " + track.getAlbum() + " (" + track.getArtist() + ")");
		}
	}

}
